package com.yoomiito.app.ui.my.angle

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.view.View
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.utils.YMToast
import com.yoomiito.app.widget.dialog.TipDialog
import com.yoomiito.app.widget.sign.SignatureView
import kotlinx.android.synthetic.main.act_sign.*
import kotlinx.android.synthetic.main.title_bar.*
import me.jessyan.autosize.AutoSizeCompat

class SignActivity :BaseActivity<SignPresenter>(){

    companion object{
        fun start(context: Context,url:String){
            val intent = Intent(context,SignActivity::class.java)
            intent.putExtra(Constant.KEY,url)
            context.startActivity(intent)
        }
    }
    private lateinit var mAgreementUrl:String
    override fun getLayoutId(): Int = R.layout.act_sign

    override fun newP(): SignPresenter = SignPresenter(App.mAppConfig)

    override fun getResources(): Resources {
        //需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
//        AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources())//如果没有自定义需求用这个方法
        AutoSizeCompat.autoConvertDensity(super.getResources(), 375f, false)//如果有自定义需求就用这个方法
        return super.getResources()
    }

    override fun initData(savedInstanceState: Bundle?) {
        mAgreementUrl = intent.getStringExtra(Constant.KEY)
        tv_center.text = "签署合同"
        tv_center.textSize = 20f
        tv_center.paint.isFakeBoldText = true
        iv_back_left.setOnClickListener { finish() }

        resign.setOnClickListener {
            writeName.clear()

        }
        commit.setOnClickListener {
            if(writeName.isEmpty) YMToast.showText("请签名") else p.uploadPhotos(writeName.signatureBitmap,mAgreementUrl)
        }
        writeName.setOnSignedListener(object :SignatureView.OnSignedListener{
            override fun onSigned() {
                tipTv.visibility = View.GONE
            }

            override fun onClear() {
                tipTv.visibility = View.VISIBLE
            }
        })
    }

    fun showTip(url: String?) {
        val tipDialog = TipDialog(this)
        tipDialog.setCanBack(false)
                .setSureText("知道了")
                .setTitle("温馨提示")
                .setContent("资料已补充完善，已正式加入天使合伙人计划！")
                .setClickListener {
                    tipDialog.dismiss()
                    MyAngleAgreementActivity.start(this,url!!)
                    finish()
                }
                .show()
        tipDialog.setCanceledOnTouchOutside(false)
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (AllCode.ANGLE_AGREEMENT_SIGN == eventMessage?.eventType){
            finish()
        }
    }

}